package kr.co.bettersoft.usedmarketandroid.adapter;

import java.util.ArrayList;

import kr.co.bettersoft.usedmarketandroid.activity.ImageViewer;
import kr.co.bettersoft.usedmarketandroid.activity.R;
import kr.co.bettersoft.usedmarketandroid.common.CommonConstant;
import kr.co.bettersoft.usedmarketandroid.common.CommonUtil;
import kr.co.bettersoft.usedmarketandroid.domain.UsedMarketChatMessage;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class UsedMarketChatMessageAdapter extends BaseAdapter{
	
	private Context context;
	private ArrayList<UsedMarketChatMessage> entries;
	private UsedMarketChatMessage usedMarketChatMessage;
	LayoutInflater layoutInflater;
	
	String TAG = "UsedMarketChatMessageAdapter";
	View listView;
	
	public UsedMarketChatMessageAdapter(Context context, ArrayList<UsedMarketChatMessage> entries) {
		this.context = context;
		this.entries = entries;
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}

	@Override
	public int getCount() {
		if(entries!=null){
			return entries.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return entries.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		listView = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(listView == null) {
			listView = layoutInflater.inflate(R.layout.usedmarket_adapter_chat_room_message_list, parent, false);
			wrapper = new ViewWrapper(listView);
			listView.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)listView.getTag();
		}
		
		TextView textViewSenderId = (TextView) listView.findViewById(R.id.chat_room_message_list_sender_id);								// 상대 닉
		TextView textViewContent = (TextView) listView.findViewById(R.id.chat_room_message_content);										// 상대 글
		TextView textViewRegDate = (TextView) listView.findViewById(R.id.chat_room_message_reg_date);										// 시각
		ImageView chat_room_other_thumb_img = (ImageView) listView.findViewById(R.id.chat_room_other_thumb_img);							// 다른 사람 프로필 썸네일
		ImageView chat_room_other_upload_file_img = (ImageView) listView.findViewById(R.id.chat_room_other_upload_file_img);		// 다른 사람이 올린 사진 썸네일

		TextView textViewMySenderId = (TextView) listView.findViewById(R.id.chat_room_message_list_my_sender_id);							// 내 닉
		TextView textViewMyContent = (TextView) listView.findViewById(R.id.chat_room_message_my_content);									// 내 글
		TextView textViewMyRegDate = (TextView) listView.findViewById(R.id.chat_room_message_my_reg_date);									// 시각
		ImageView chat_room_my_upload_file_img = (ImageView) listView.findViewById(R.id.chat_room_my_upload_file_img);			// 내가 올린 사진 썸네일

		View my_content = listView.findViewById(R.id.my_content);					// 오른쪽(내가 쓴 글)
		View not_my_content = listView.findViewById(R.id.not_my_content);			// 왼쪽(상대가 쓴 글)

		if(((UsedMarketChatMessage)entries.get(position)).getMyMessageYn().equals("Y")){
			my_content.setVisibility(View.VISIBLE);
			not_my_content.setVisibility(View.GONE);
			//			textViewMySenderId.setText(((UsedMarketChatMessage)entries.get(position)).getPushChatUserMemberId());
//			textViewMySenderId.setText(((UsedMarketChatMessage)entries.get(position)).getNickName());		// ### 
			textViewMyContent.setText(((UsedMarketChatMessage)entries.get(position)).getContent());
			textViewMyRegDate.setText(((UsedMarketChatMessage)entries.get(position)).getRegisterDate());

			// 업로드 이미지  // ###   
			if(((UsedMarketChatMessage)entries.get(position)).getUploadFileThumbnailImg()!=null){
				chat_room_my_upload_file_img.setImageBitmap(((UsedMarketChatMessage)entries.get(position)).getUploadFileThumbnailImg());
				chat_room_my_upload_file_img.setVisibility(View.VISIBLE);
				chat_room_my_upload_file_img.setTag(position);
				//				chat_room_my_upload_file_img.setOnClickListener(new OnClickListener() {
				//					@Override
				//					public void onClick(View v) {
				//						// TODO Auto-generated method stub
				//						Log.d(TAG,"image touched");
				//					}
				//				});
			}else{
				chat_room_my_upload_file_img.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
				chat_room_my_upload_file_img.setVisibility(View.GONE);
				//				Log.d(TAG,"my upload file not exist/"+position);
			}

			textViewSenderId.setText("");
			textViewContent.setText("");
			textViewRegDate.setText("");
		}else{
			my_content.setVisibility(View.GONE);
			not_my_content.setVisibility(View.VISIBLE);
			//			textViewSenderId.setText(((UsedMarketChatMessage)entries.get(position)).getPushChatUserMemberId());
//			textViewSenderId.setText(((UsedMarketChatMessage)entries.get(position)).getNickName());		// ### 
			textViewContent.setText(((UsedMarketChatMessage)entries.get(position)).getContent());
			textViewRegDate.setText(((UsedMarketChatMessage)entries.get(position)).getRegisterDate());

//			// 상대방 프로필 썸네일  // ### 
//			if( (((UsedMarketChatMessage)entries.get(position)).getProfileThumbnailImg())!= null ){
//				chat_room_other_thumb_img.setImageBitmap(((UsedMarketChatMessage)entries.get(position)).getProfileThumbnailImg());
//			}else{	// 없으면 기본 이미지
//				chat_room_other_thumb_img.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty_member));
//			}
			// 업로드 이미지  
			if(((UsedMarketChatMessage)entries.get(position)).getUploadFileThumbnailImg()!=null){
				//				Log.d(TAG,position+"/ file exist/"+((UsedMarketChatMessage)entries.get(position)).getUploadFileThumbnailImg().getHeight());
				chat_room_other_upload_file_img.setImageBitmap(((UsedMarketChatMessage)entries.get(position)).getUploadFileThumbnailImg());
				chat_room_other_upload_file_img.setVisibility(View.VISIBLE);
				chat_room_other_upload_file_img.setTag(position);
				//				chat_room_other_upload_file_img.setOnTouchListener(new OnTouchListener() {
				//					@Override
				//					public boolean onTouch(View v, MotionEvent event) {
				//						Log.d(TAG,"chat_room_other_upload_file_img touched");
				//						// TODO Auto-generated method stub
				//						return false;
				//					}
				//				});
			}else{
				chat_room_other_upload_file_img.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
				chat_room_other_upload_file_img.setVisibility(View.GONE);
				//				Log.d(TAG,"other upload file not exist/"+position);
			}
			textViewMySenderId.setText("");
			textViewMyContent.setText("");
			textViewMyRegDate.setText("");
		}


		// ### 
		chat_room_my_upload_file_img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ImageViewer.class);
				intent.putExtra("imageUrl", CommonConstant.messageImgPathOriginal +((UsedMarketChatMessage)entries.get(Integer.parseInt(v.getTag()+""))).getMessageImage());	
				context.startActivity(intent);

				//				context.startActivity(intent);
				//				
				//				ChatRoomActivity chatRoomActivity = (ChatRoomActivity) ChatRoomActivity.chatRoomActivity;
				//				chatRoomActivity.showImageToLarge(v.getTag()+"");
				////				Log.d(TAG,"chat_room_my_upload_file_img.setOnClickListener//"+v.getTag());
			}
		});
		chat_room_other_upload_file_img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ImageViewer.class);
				intent.putExtra("imageUrl", CommonConstant.messageImgPathOriginal +((UsedMarketChatMessage)entries.get(Integer.parseInt(v.getTag()+""))).getMessageImage());
				context.startActivity(intent);
				//				ChatRoomActivity chatRoomActivity = (ChatRoomActivity) ChatRoomActivity.chatRoomActivity;
				//				chatRoomActivity.showImageToLarge(v.getTag()+"");
				////				Log.d(TAG,"chat_room_other_upload_file_img.setOnClickListener//"+v.getTag());
			}
		});
		
		return listView;
	}
	
	class ViewWrapper {
		View base;
		ViewWrapper(View base) {
			this.base = base;
		}
		
	}

}